﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Web.Profile;
using System.Data.Linq;

namespace IPMS.Models
{
    public class CustomProfileProvider : ProfileProvider
    {
        private DataContextDataContext dataContext = new DataContextDataContext();

        // method to get user is authenticated profile
        public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
        {
            //get user by username from SettingsContext
            string username = context["UserName"].ToString();
            User userLogined = this.dataContext.Users.FirstOrDefault(u => u.Username == username);

            SettingsPropertyValueCollection values = new SettingsPropertyValueCollection();
            foreach (SettingsProperty property in collection)
            {
                SettingsPropertyValue value = new SettingsPropertyValue(property);
                switch (property.Name)
                {
                    case "ID":
                        value.PropertyValue = userLogined.ID;
                        break;
                   
                    case "Fullname":
                        value.PropertyValue = userLogined.Fullname;
                        break;
                    case "Email":
                        value.PropertyValue = userLogined.Email;
                        break;
                    case "Phone":
                        value.PropertyValue = userLogined.Phone;
                        break;
                    case "DeparmentID":
                        value.PropertyValue = userLogined.DepartmentID;
                        break;
                    case "RoleID":
                        value.PropertyValue = userLogined.RoleID;
                        break;
                    case "Actived":
                        value.PropertyValue = userLogined.Actived;
                        break;
                    case "CreatedDate":
                        value.PropertyValue = userLogined.CreatedDate;
                        break;                 
                    default:
                        value.PropertyValue = "undetified";
                        break;

                }
                values.Add(value);
            }
            return values;
        }

        #region override methods of ProfileProvider
        public override int DeleteInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
        {
            throw new NotImplementedException();
        }

        public override int DeleteProfiles(string[] usernames)
        {
            throw new NotImplementedException();
        }

        public override int DeleteProfiles(ProfileInfoCollection profiles)
        {
            throw new NotImplementedException();
        }

        public override ProfileInfoCollection FindInactiveProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override ProfileInfoCollection FindProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override ProfileInfoCollection GetAllInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override ProfileInfoCollection GetAllProfiles(ProfileAuthenticationOption authenticationOption, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override int GetNumberOfInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        
        public override void SetPropertyValues(System.Configuration.SettingsContext context, System.Configuration.SettingsPropertyValueCollection collection)
        {
            return;
        }
        #endregion
    }
}